import { defineStore } from 'pinia'
import _ from 'lodash-es'
import api from '../request/api'
import { get } from '../request/index'
import dict from '../utils/dict'

// 你可以任意命名 `defineStore()` 的返回值，但最好使用 store 的名字，同时以 `use` 开头且以 `Store` 结尾。
// (比如 `useUserStore`，`useCartStore`，`useProductStore`)
// 第一个参数是你的应用中 Store 的唯一 ID。
export const useLayoutStore = defineStore('layout', {
  // 其他配置...
  state: () => ({ layout: [] }),
  getters: {
    getLayout: (state) => state.layout
  },
  actions: {
    addGridItem(item) {
      const { key, width, height, maxH, minW } = item
      this.layout.push({
        x: 0,
        y: 0,
        w: width,
        h: height,
        maxH,
        minW,
        i: key,
        component: key
      })
    },
    delGridItem(item) {
      this.layout = _.difference(this.layout, [item])
    },
    exist(i) {
      return _.findIndex(this.layout, (v) => v.i === i) !== -1
    }
  }
})

export const userBasicInfoStore = defineStore('basicInfo', {
  state: () => ({ basicInfo: {}, environment: '' }),
  getters: {
    getBasicInfo: (state) => state.basicInfo,
    getEnvironment: (state) => {
      return state.environment
        ? dict.environment[state.environment?.slice(0, 2)][
            (state.environment?.slice(2, state.environment?.length) || 1) - 1
          ]
        : '--'
    }
  },
  actions: {
    async fetchUserBasicInfo() {
      this.basicInfo = await get(api().lcuApi.curUserInfo)
    },
    async fetchUserEnvironment() {
      const { environment } = await get(api().lcuApi.getEnvironment)
      this.environment = environment
    }
  }
})
